home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Bug Proofing Visual Basic: A Guide to Error Handling and Prevention
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471323519
Publication Date: 11/01/98

Search this book:
 
Previous Table of Contents Next


This version performs each task only once.

If move_to_origin Then
    ‘ Translate the object to the origin.
    MoveToOrigin Polytope
End If

‘ Scale, rotate, and project the object.
ScaleObject Polytope, object_scale
RotateObject Polytope, object_rotation
PerspectiveProject Polytope, CenterOfProjection

If move_to_origin Then
    ‘ Translate the back to its original position.
    MoveBack Polytope
End If

Notice that the second version uses the same If statement twice. If this statement involves a complex expression, use a variable to simplify it as in this example.

Encapsulate Library Calls

Place library declarations and calls in separate modules. Place API declarations and function calls in one module, declarations for an expression parsing library in another, and so forth. Write Visual Basic routines to encapsulate the library calls so the main program does not depend on the details of how they work.

Library calls sometimes change when a new version of a library is released. If the calls are encapsulated in a module, you can easily review them to see if you need to make changes whenever you install a new version of the library. If a call has changed, you can change the wrapper routine you built in a single place instead of looking for calls scattered throughout the application.


Figure 10.2  Program Bad10 manipulates a stack.

Self-Test

Program Bad10, shown in Figure 10.2, violates some of the guidelines described in this chapter. This program implements a stack , a kind of list where items are added and removed from the same end of the list. Because items are removed in last-in-first-out order, stacks are sometimes called LIFO lists or LIFOs.

For historical reasons, adding an item to a stack is called pushing the item onto the stack. Removing the top item from the stack is called popping the item off of the stack.

Enter values in the program’s Last Name and First Name fields and click the Push button to add an item to the stack. Click the Pop button to remove the most recently added item.

Program Bad10 is fairly simple, but it disobeys several of the rules described in this chapter. Such a simple program can get away with some of these violations. In a more complex program, however, they could cause a lot of trouble. The source code for program Bad10 follows. Appendix A, “Self-Test Solutions,” contains an improved version of the program.

Option Explicit

‘ The data for a person.
Private NumPeople As Integer
Private LastNames() As String
Private FirstNames() As String

‘ Add an item to the stack.
Private Sub cmdPush_Click()

    ‘ Create space for the new person.
    ReDim Preserve LastNames(1 To NumPeople + 1)
    ReDim Preserve FirstNames(1 To NumPeople + 1)

    ‘ Save the new person’s data.
    LastNames(NumPeople + 1) = txtLastName.Text
    FirstNames(NumPeople + 1) = txtFirstName.Text
    txtLastName.Text = “”
    txtFirstName.Text = “”

    ‘ Enable the Pop button.
    cmdPop.Enabled = True

    ‘ Update the number of people.
    NumPeople = NumPeople + 1

    ‘ Display the list.
    DisplayList
End Sub

‘ Remove the top item from the stack.
Private Sub cmdPop_Click()

    ‘ Display the person’s values.
    txtLastName.Text = LastNames(NumPeople)
    txtFirstName.Text = FirstNames(NumPeople)

    ‘ Remove the last person.
    If NumPeople - 1 > 0 Then
        ReDim Preserve LastNames(1 To NumPeople - 1)
        ReDim Preserve FirstNames(1 To NumPeople - 1)
    Else
        Erase LastNames
        Erase FirstNames
    End If

    ‘ Enable this button only if there are
    ‘ people left.
     cmdPop.Enabled = (NumPeople - 1 > 0)

    ‘ Save the new number of people.
    NumPeople = NumPeople - 1

    ‘ Display the list.
    DisplayList
End Sub

‘ Display the people in the stack.
Private Sub DisplayList()
Dim i As Integer
Dim txt As String

    For i = 1 To NumPeople
        txt = LastNames(i) & “, ” & _
            FirstNames(i) & _
            vbCrLf & txt
    Next i
    txtList.Text = txt

    ‘ The last name field is where the user will
    ‘ start editing the new data.
    txtLastName.SetFocus
End Sub

Summary

Encapsulation hides detail from code that does not need to know about it. Encapsulation lets you concentrate on one piece of code while ignoring irrelevant complexity.

There are many ways you can take advantage of encapsulation in your programs. The key is to represent complex things with simpler things. Use variables to represent complex expressions or object references. Use UDTs to represent complicated data structures. Use subroutines, classes, and modules to represent even more complex entities and behaviors. Take advantage of encapsulation in all its forms.

The following Bug Stoppers summarize the encapsulation concepts explained in this chapter.

BUG STOPPERS: Encapsulation
Hide the internals of a routine from other routines.
Encapsulate with classes.
Look for memory leaks by counting object creation and destruction.
Initialize objects.
Use property procedures instead of public variables.
Use the Implements keyword to define interfaces and improve performance.
Encapsulate with user-defined types.
Encapsulate with controls.
Encapsulate with subroutines.
Encapsulate with modules.
Use variables to avoid recalculating expressions.
Use variables to avoid recalculating property values.
Keep as much private as possible in classes and modules.
Use With to simplify using objects.
Use variables instead of nested With statements.
Do not repeat code.
Encapsulate library calls so you can easily change them later.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.